<template>
  <div v-loading="loading" class="bg-white p-4 rounded">
    <el-form :model="form" label-width="160px">
      <h5 class="bg-gray-100 p-3 rounded mb-5">基础设置</h5>
           <el-form-item label="分销启用">
            <el-radio-group v-model="form.distribution_open">
              <el-radio :value="0" border>
                禁用
              </el-radio>
              <el-radio :value="1" border>
                开启
              </el-radio>
            </el-radio-group>
          </el-form-item>
           <el-form-item label="分销海报图">
              <ChooseImage :limit="9" v-model="form.spread_banners" />
           </el-form-item>

      <h5 class="bg-gray-100 p-3 rounded mb-5">返佣设置</h5>
          <el-form-item label="一级返佣比例">
            <div>
              <el-input v-model="form.store_first_rebate" placeholder="一级返佣比例" style="width: 50%;" type="number">
                <template #append>%</template>
              </el-input>
              <small class="text-gray-500 flex mt-1">订单交易成功</small>
            </div>
          </el-form-item>
          <el-form-item label="二级返佣比例">
            <div>
              <el-input v-model="form.store_second_rebate" placeholder="二级返佣比例" style="width: 50%;" type="number">
                <template #append>%</template>
              </el-input>
              <small class="text-gray-500 flex mt-1">订单交易成功</small>
            </div>
          </el-form-item>
          <el-form-item label="自购返佣">
            <div>
               <el-radio-group v-model="form.is_self_brokerage">
              <el-radio :value="0" border>
                否
              </el-radio>
              <el-radio :value="1" border>
                是
              </el-radio>
            </el-radio-group>
            <small class="text-gray-500 flex mt-1">订单交易成功</small>
            </div>
          </el-form-item>
          
      <h5 class="bg-gray-100 p-3 rounded mb-5">结算设置</h5>
          <el-form-item label="结算时间">
            <div>
              <el-input v-model="form.settlement_days" placeholder="结算时间" 
              style="width: 80%;" type="number">
                <template #prepend>订单完成后</template>
                <template #append>天</template>
              </el-input>
              <small class="text-gray-500 flex mt-1">订单交易成功</small>
            </div>
          </el-form-item>
          <el-form-item label="佣金到账时间">
            <div>
               <el-radio-group v-model="form.brokerage_method">
              <el-radio value="hand" border>
                手动到账
              </el-radio>
              <el-radio value="wx" border>
                自动到微信零钱
              </el-radio>
            </el-radio-group>
            <small class="text-gray-500 flex mt-1">订单交易成功</small>
            </div>
          </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue"
import {
  getConfig,
  setConfig
} from "@/api/distribution.js"
import ChooseImage from '@/components/ChooseImage.vue'
import { toast } from "@/composables/util"


const form = reactive({
  "distribution_open": 1,
  "store_first_rebate": 10,
  "store_second_rebate": 20,
  "spread_banners": [
    "http://...png",
    // ...
  ],
  "is_self_brokerage": 1,
  "settlement_days": 7,
  "brokerage_method": "hand"
})

const loading = ref(false)

//先处理数据，form内部数据改变之后，会影响html部分数据的渲染
function getData() {
  loading.value = true
  getConfig().then(res => {
    for (const k in form) {
      form[k] = res[k]
    }
  }).finally(() => {
    loading.value = false
  })
}
getData()//渲染默认选中

const submit = () => {
  loading.value = true
  setConfig({
    //后端传递该数据是以字符串的形式，使用解构语法可以覆盖特定字段
    ...form,
  }).then(res => {
    toast("修改成功")
    getData()
  }).finally(() => {
    loading.value = false
  })
}
</script>